package at.austria.leecher;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

public class Example {

    private static final String URL_OE3_AUSTRIA_SINGLE_CHARTS = "http://oe3.orf.at/charts/stories/single";

    public static void main( String[] args ) {
        WebDriver driver = new FirefoxDriver();

        try {
            driver.get( URL_OE3_AUSTRIA_SINGLE_CHARTS );

            final WebElement tableCharts = driver.findElement( By.xpath( "//table[@class='charts']" ) );
            final WebElement body = tableCharts.findElement( By.tagName( "tbody" ) );
            final List<WebElement> entries = body.findElements( By.tagName( "tr" ) );

            for ( final WebElement entry : entries ) {
                final List<WebElement> columns = entry.findElements( By.tagName( "td" ) );

                final int rank = Integer.valueOf( columns.get( 0 ).getText() );
                final String interpret = columns.get( 4 ).getText();
                final String album = columns.get( 5 ).getText();

                System.out.println( String.format( "%2d: %s - %s", rank, interpret, album ) );
            }

        } finally {
            driver.close();
        }
    }
}